package com.example.mybatisplusdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplusdemo.common.utls.SessionUtils;
import com.example.mybatisplusdemo.model.domain.User;
import com.example.mybatisplusdemo.mapper.UserMapper;
import com.example.mybatisplusdemo.model.dto.PageDTO;
import com.example.mybatisplusdemo.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lxp
 * @since 2025-06-17
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private UserMapper userMapper;

//    @Override
//    public Page<User> pageQuery(int pageNum, int pageSize, String login_name, String password) {
//        // 创建 MyBatis-Plus 分页对象
//        Page<User> page = new Page<>(pageNum, pageSize);
//
//        // 构建动态查询条件
//        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//
//        if (StringUtils.hasText(login_name)) {
//            queryWrapper.like("login_name", login_name);
//        }
//
//        if (StringUtils.hasText(password)) {
//            queryWrapper.eq("password", password);
//        }
//
//        // 执行分页查询
//        Page<User> resultPage = userMapper.selectPage(page, queryWrapper);
//
//        // 转换为自定义的分页结果对象
//        return new Page<>();

        @Override
        public List<User> listByKey(String key){
            LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
            wrapper.like(User::getLoginName, key);
            List<User> users = userMapper.selectList(wrapper);
            return users;
    }

    @Override
    public User login(User user){
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getLoginName, user.getLoginName()).eq(User::getPassword, user.getPassword());
        User one = userMapper.selectOne(wrapper);
        //应该把用户存进session
        SessionUtils.saveCurrentUserInfo(one);
        return one;
    }

    @Override
    public Page<User> listPage(PageDTO pageDTO, User user) {
        Page<User> page = new Page<>(pageDTO.getPageNo(), pageDTO.getPageSize());
        page = userMapper.listPage(page, user);
        return page;
    }


}
